package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import java.util.Date;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
/**
 * 体检预约服务
 */

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Override
    public Result order(Map map) throws Exception {
        //检查当前的日期是否进行了预约设置
        //获取时间
        String orderDate = (String)map.get("orderDate");
        //判断是否为空并将字符串类型装换成date类型
        Date date = DateUtils.parseString2Date(orderDate);
        //调用数据库查询当天的预约信息
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        //判断查询结果是否为空
        if (orderSetting == null){
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查预约时间是否预约已满
        //获取可预约人数
        int number = orderSetting .getNumber();
        //获取以预约人数
        int reservations = orderSetting.getReservations();
        //判断预约人数是否已满
        if (reservations >= number){
            //如果满员返回false 和 满员信息
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //检查当前用户是否为会员 根据手机号判断
        //获取手机号
        String telephone = (String) map.get("telephone");
        //在数据库中查询手机号是否已经存在  返回的是会员的而实体类
        Member member = memberDao.findByTelephone(telephone);
        //防止重复预约
        //判断会员实体类是否为空
        if (member != null){
            //是会员
            //获取id
            Integer memberId = member.getId();
            //获取套餐id
            int setmealId = Integer.parseInt((String) map.get("setmealId"));
            //转换日期格式
            Date order_date = DateUtils.parseString2Date(orderDate);
            //创建order对象 将以上查询到的信息赋值给order对象
            Order order =  new Order(memberId,order_date,setmealId);
            //查询预约表判断是否为空,如果为空重新赋值,,返回list集合
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            //不是会员则添加会员
            //new 一个新的会员对象重新赋值,并添加到数据库
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //5、预约成功，更新当日的已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        orderDao.add(order);


        //可以预约 设置预约人数加一
        orderSetting.setReservations(orderSetting.getReservations()+1);
        //在数据更新可预约人数
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        //返回成功信息的同时返回会员的id 以供预约成功页面使用
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());

    }


    @Override
    public Map findById(Integer id) throws Exception{
        Map map = orderDao.findById4Detail(id);
        if (map != null){
            //处理日期格式
            //三张表的联查操作=
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }


}
